import 'package:flutter/material.dart';

import 'package:go_router/go_router.dart';

import '../../MyApp.dart';
import 'ManageStateMix.dart';
import 'ManageStateParent.dart';
import 'ManageStateSelf.dart';

List<Item> listData = [
  Item("ManageStateMix", "ManageStateMix"),
  Item("ManageStateParentWidget", "ManageStateParentWidget"),
  Item("ManageStateSelf", "ManageStateSelf")
];

/// The route configuration.
final GoRouter _router = GoRouter(
  routes: <RouteBase>[
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return const _HomeScreen();
      },
      routes: <RouteBase>[

        GoRoute(
          path: 'ManageStateMix',
          builder: (BuildContext context, GoRouterState state) {
            return const ManageStateMix();
          },
        ),
        GoRoute(
          path: 'ManageStateParentWidget',
          builder: (BuildContext context, GoRouterState state) {
            return const ManageStateParentWidget();
          },
        ),
        GoRoute(
          path: 'ManageStateSelf',
          builder: (BuildContext context, GoRouterState state) {
            return const ManageStateSelf();
          },
        )
      ],
    ),
  ],
);

/// The home screen
class _HomeScreen extends StatelessWidget {
  /// Constructs a [_HomeScreen]
  const _HomeScreen();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('TAB')),
      body: ListView.builder(
        itemCount: listData.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text(listData[index].title),
            onTap: () {
              context.go("/${listData[index].path}");
              for (var value in listData) {
                print(value);
              }
            },
          );
        },
      ),
    );
  }
}

/// The main app.
class StateDemoList extends StatelessWidget {
  /// Constructs a [TabDemoList]
  const StateDemoList({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
    );
  }
}
